package com.todomvc.tests;

import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;

import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.todomvc.fw.NavigationHelper;
import com.todomvc.fw.Todo;
import com.todomvc.fw.TodoHelper;
import com.todomvc.fw.Todos;

public class TodoDestroyTests extends TestBase {

	private NavigationHelper navMan;
	private TodoHelper todoMan;

	
	@BeforeClass
	public void getManagers () {
		todoMan = manager.getTodoHelper();
		navMan = manager.getNavigationHelper();
		navMan.openAngularPage();
	}
	
	
	@Test
	public void lastTodoShouldBeDestroyed () {

		// Adding an item to ensure that we have something to be destroyed
		todoMan.add(new Todo("Say something bad"));

		Todos oldList = todoMan.getTodos();
		todoMan.destroyLast();
		Todos newList = todoMan.getTodos();
		assertThat(newList, equalTo(oldList.withoutLast()));
	}

	
	@Test
	public void firstTodoShouldBeDestroyed () {

		// Adding 2 items to ensure that we have something to be destroyed and at least one item will survive after destroying
		todoMan.add(new Todo("Say first word"));
		todoMan.add(new Todo("Say second word"));

		Todos oldList = todoMan.getTodos();
		todoMan.destroyFirst();
		Todos newList = todoMan.getTodos();
		assertThat(newList, equalTo(oldList.withoutFirst()));
	}

	
	@Test
	public void anyTodoShouldBeDestroyed () {

		// Adding 3 items to ensure that we have something not first and not last to be destroyed
		todoMan.add(new Todo("Say first word"));
		todoMan.add(new Todo("Say second word"));
		todoMan.add(new Todo("Say third word"));

		// will destroy second element 
		int index = 2;
		
		Todos oldList = todoMan.getTodos();
		todoMan.destroyNth(index);
		Todos newList = todoMan.getTodos();
		assertThat(newList, equalTo(oldList.withoutNts(index)));
	}

	
	@Test
	public void allTodosShouldBeDestroyed () {
	
		// Adding 3 items to ensure rule of three
		todoMan.add(new Todo("Say first word"));
		todoMan.add(new Todo("Say second word"));
		todoMan.add(new Todo("Say third word"));

		Todos oldList = todoMan.getTodos();
		
		for (int i = 0; i < oldList.size(); i++) {
			todoMan.destroyFirst();
		}
		Todos newList = todoMan.getTodos();
		assertThat(newList.size(), equalTo(0));
	}

	@Test
	public void todoCountShouldBeUpdatedOnDestroying () {
		todoMan.add(new Todo("Say wow!"));
		int oldTodoCount = todoMan.getTodoCount();
		todoMan.destroyLast();
		int newTodoCount = todoMan.getTodoCount();
		assertThat(newTodoCount, equalTo(oldTodoCount - 1));
	}
}
